package com.example.demo.controller.admin;

import com.example.demo.Constant.StatusConstant;
import com.example.demo.controller.BaseController;
import com.example.demo.dao.ClassesDao;
import com.example.demo.dao.ResourceDao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.dto.QueryDTO;
import com.example.demo.entity.pojo.Classes;
import com.example.demo.entity.pojo.Resource;
import com.example.demo.entity.pojo.User;
import com.example.demo.entity.vo.PageVO;
import com.example.demo.entity.vo.ResourceVO;
import com.example.demo.entity.vo.ResponseVO;
import com.example.demo.entity.vo.UserVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController extends BaseController {
    @Autowired
    ResourceDao resourceDao;
    @Autowired
    UserDao userDao;
    @Autowired
    ClassesDao classesDao;
    private static final String YI_CHANG = "该教师账号未启用，请重新选择";
    /**
     * 新增课件资源
     * @param resource
     * @return
     */
    @PostMapping
    public ResponseVO save(@RequestBody Resource resource) {
        //分情况判断
        //该教师账号是否正常启用
        //根据teacherId查询教师
        /*User user = userDao.getById(resource.getTeacherId());
        if (!user.getStatus().equals(StatusConstant.ENABLE)) {
            //教师账号未启用
            return getFail(YI_CHANG);
        }*/
        //教师账号启用中
        resourceDao.save(resource);
        return getSuccess(null , null);
    }

    /**
     * 课件资源分类查询(多表查询)
     * @param queryDTO
     * @return
     */
    @GetMapping("/list")
    public ResponseVO<PageVO> page(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage() , queryDTO.getPageSize());
        Page<ResourceVO> page = resourceDao.pageQuery(queryDTO);
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setRecords(page.getResult());
        return getSuccess(null , pageVO);
    }

    /**
     * 根据id查询课件资源
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseVO<ResourceVO> getById(@PathVariable Long id) {
        ResourceVO resourceVO = resourceDao.getById(id);
        return getSuccess(null , resourceVO);
    }

    /**
     * 获取所有账号启用的教师，用于添加或修改课件
     * @return
     */
    @GetMapping("/teacher")
    public ResponseVO<List<UserVO>> getAllTeachers() {
        List<UserVO> list = userDao.getAllEnable();
        return getSuccess(null , list);
    }

    /**
     * 获取所有班级，用于添加或修改课件
     * @return
     */
    @GetMapping("/classes")
    public ResponseVO<List<Classes>> getAllClasses() {
        List<Classes> list = classesDao.queryAll();
        return getSuccess(null , list);
    }

    /**
     * 修改课件资源
     * @param resource
     * @return
     */
    @PutMapping
    public ResponseVO update(@RequestBody Resource resource) {
        resourceDao.update(resource);
        return getSuccess("修改成功" , null);
    }

    /**
     * 根据id删除课件资源
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseVO delete(@PathVariable Long id) {
        resourceDao.deleteById(id);
        return getSuccess("删除成功" , null);
    }
}
